import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:starfish_tenement_app/component/styles/app_colors.dart';

import '../../utils/string_utils.dart';
import '../title/app_text.dart';

//咨询卡片样式
enum NewsCardType {
  //方形
  square,
  //圆角
  circle
}

class NewsCardItem extends StatelessWidget {
  const NewsCardItem(
      {super.key,
      required this.newsCardType,
      this.coverImageUrl,
      required this.content,
      this.avatarUrl,
      this.nickname,
      this.createTime,
      this.onTap,
      this.likeTap});

  final NewsCardType newsCardType;

  //封面图片地址
  final String? coverImageUrl;

  //文本内容
  final String content;

  //头像
  final String? avatarUrl;

  //用户昵称
  final String? nickname;

  //创建时间
  final String? createTime;

  //点击事件
  final GestureTapCallback? onTap;

  //点赞事件
  final GestureTapCallback? likeTap;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.only(bottom: 5.h),
      decoration: BoxDecoration(
          borderRadius: newsCardType == NewsCardType.circle
              ? BorderRadius.circular(12.h)
              : BorderRadius.zero,
          border: Border.all(color: Colors.grey)),
      child: ClipRRect(
        borderRadius: newsCardType == NewsCardType.circle
            ? BorderRadius.all(Radius.circular(12.h))
            : BorderRadius.zero,
        child: Column(
          children: [
            //图片封面和正文
            GestureDetector(
              onTap: onTap,
              child: Column(children: [
                //封面
                _getImage(coverImageUrl, fit: BoxFit.fitWidth),
                SizedBox(height: 10.h),
                //正文
                Container(
                  margin: EdgeInsets.only(left: 5.h, right: 5.h),
                  child: AppText(
                    text: content,
                    overflow: TextOverflow.ellipsis,
                    fontSize: 12.sp,
                    textColor: AppColors.textColor20,
                    maxLines: 2,
                  ),
                )
              ]),
            ),
            //图像、昵称、创建时间、点赞icon
            _cardItemBar(),
          ],
        ),
      ),
    );
  }

  ///图像、昵称、创建时间、点赞icon
  Widget _cardItemBar() {
    return Row(children: [
      SizedBox(width: 5.h),
      ClipOval(
        child:
            _getImage(avatarUrl, width: 30.h, height: 30.h, fit: BoxFit.fill),
      ),
      Column(
        children: [
          AppText(
              text: nickname ?? "",
              fontSize: 12.sp,
              textColor: AppColors.textColorB7),
          AppText(
              text: createTime ?? "",
              fontSize: 12.sp,
              textColor: AppColors.textColorB7),
        ],
      ),
      //点赞ICON
      Expanded(
        child: Align(
          alignment: Alignment.centerRight,
          child: GestureDetector(
            onTap: likeTap,
            child: Image.asset("assets/images/icon_news_type_dianzan.png",
                width: 20.r, height: 20.r),
          ),
        ),
      ),
      SizedBox(width: 10.h)
    ]);
  }

  ///处理网络图片加载失败
  Widget _getImage(imageUrl, {width, height, fit}) {
    if (StringUtils.isEmpty(imageUrl)) {
      return const SizedBox();
    }
    if (width != null && height != null) {
      return FadeInImage(
          image: NetworkImage(imageUrl),
          width: width,
          height: height,
          fit: fit,
          placeholder:
              const AssetImage("assets/images/icon_news_type_dianzan.png"),
          imageErrorBuilder: (context, error, stackTrace) {
            return Image.asset("assets/images/icon_news_type_dianzan.png",
                fit: fit);
          });
    }
    if (width != null) {
      return FadeInImage(
          image: NetworkImage(imageUrl),
          width: width,
          fit: fit,
          placeholder:
              const AssetImage("assets/images/icon_news_type_dianzan.png"),
          imageErrorBuilder: (context, error, stackTrace) {
            return Image.asset("assets/images/icon_news_type_dianzan.png",
                fit: fit);
          });
    }
    if (height != null) {
      return FadeInImage(
          image: NetworkImage(imageUrl),
          height: height,
          fit: fit,
          placeholder:
              const AssetImage("assets/images/icon_news_type_dianzan.png"),
          imageErrorBuilder: (context, error, stackTrace) {
            return Image.asset("assets/images/icon_news_type_dianzan.png",
                fit:fit);
          });
    }
    return FadeInImage(
        image: NetworkImage(imageUrl),
        fit: fit,
        placeholder:
            const AssetImage("assets/images/icon_news_type_dianzan.png"),
        imageErrorBuilder: (context, error, stackTrace) {
          return Image.asset("assets/images/icon_news_type_dianzan.png",
              fit: fit);
        });
  }
}
